<?php

class SettingController extends Controller {
    public $layout = 'setting';

    // default page
    public function actionDefault() {
//        CVarDumper::dump(Yii::app()->params, 11, true);
        $this->layout = 'contents';
        $this->render('default');
    }

    // 主界面
    public function actionIndex() {
        $this->renderPartial('index', null, false, true);
    }
    
    // 前台应用设置
    public function actionFront() {
        $this->layout = 'contents';
        if (Yii::app()->request->isPostRequest) {
            $configs = $_POST;
            unset($configs['submit_setting']);

            // begin generate online customer service js file
            $online_customer_service = trim($_POST['online_customer_service']);
            Yii::import('ext.YHelper.YOnlineCustomerService');
            $online = new YOnlineCustomerService();
            $c = $online->generateIMDatas();
            $online->writeJsFile($c);
            // end generate online customer service js file


//            CVarDumper::dump($online_customer_service, 10, true);
//            CVarDumper::dump($configs, true, 10);
//            exit;
//            $data = $_POST;
            $enter_characters = "\r\n";
            $path = Yii::app()->getBasePath();
            $cachefile = $path . '/config/config_frontend.php';
            $cachetext = "<?php{$enter_characters}// Auto generate in "
                . date('Y-m-d H:m:s', time()) . ' by system.' . $enter_characters
                . '// Please don\'t manual modify it!!!' . $enter_characters
                . 'return ' . $this->arrayeval($configs) . ";{$enter_characters}";
            //var_dump($cachetext);die($cachefile);
            if (!$this->swritefile($cachefile, $cachetext)) {
                die("File: $cachefile write error.");
            } else {
                $this->redirect($this->createUrl('setting/upload#setting_front'));
            }
        }
        $this->render('default');
    }

    // 系统设置
    public function actionSystem() {
        $this->layout = 'contents';
        if (Yii::app()->request->isPostRequest) {
            $configs = $_POST;
            unset($configs['submit_setting']);
//            CVarDumper::dump($configs, true, 10);
//            $data = $_POST;
            $enter_characters = "\r\n";
            $path = Yii::app()->getBasePath();
            $cachefile = $path . '/config/config_backend.php';
            $cachetext = "<?php{$enter_characters}// Auto generate in "
                . date('Y-m-d H:m:s', time()) . ' by system.' . $enter_characters
                . '// Please don\'t manual modify it!!!' . $enter_characters
                . 'return ' . $this->arrayeval($configs) . ";{$enter_characters}";
            //var_dump($cachetext);die($cachefile);
            if (!$this->swritefile($cachefile, $cachetext)) {
                die("File: $cachefile write error.");
            } else {
                $this->redirect($this->createUrl('setting/upload#setting_system'));
            }
        }
        $this->render('default');
    }

    // 上传设置
    public function actionUpload() {
        $this->layout = 'contents';
        if (Yii::app()->request->isPostRequest) {
            $configs = $_POST;
            unset($configs['submit_setting']);
//            CVarDumper::dump($configs, true, 10);
//            $data = $_POST;
            $enter_characters = "\r\n";
            $path = Yii::app()->getBasePath();
            $cachefile = $path . '/config/config_upload.php';
            $cachetext = "<?php{$enter_characters}// Auto generate in "
                . date('Y-m-d H:m:s', time()) . ' by system.' . $enter_characters
                . '// Please don\'t manual modify it!!!' . $enter_characters
                . 'return ' . $this->arrayeval($configs) . ";{$enter_characters}";
            //var_dump($cachetext);die($cachefile);
            if (!$this->swritefile($cachefile, $cachetext)) {
                die("File: $cachefile write error.");
            } else {
                $this->redirect($this->createUrl('setting/upload#setting_upload'));
            }
        }
        $this->render('default');
    }

    // 节点模块设置
    public function actionNodeModule() {
        $this->layout = 'contents';
        if (Yii::app()->request->isPostRequest) {
            $configs = $_POST;
            unset($configs['submit_setting']);
            $temps = Node::model()->typeOptions;
            $types = array();
            foreach ($temps as $key => $value) {
                echo $key;
                $types[$key] = (isset($configs[$key])) ? 1 : 0;
            }
            $enter_characters = "\r\n";
            $path = Yii::app()->getBasePath();
            $cachefile = $path . '/config/config_nodeModule.php';
            $cachetext = "<?php{$enter_characters}// Auto generate in "
                . date('Y-m-d H:m:s', time()) . ' by system.' . $enter_characters
                . '// Please don\'t manual modify it!!!' . $enter_characters
                . 'return ' . $this->arrayeval($types) . ";{$enter_characters}";
            //var_dump($cachetext);die($cachefile);
            if (!$this->swritefile($cachefile, $cachetext)) {
                die("File: $cachefile write error.");
            } else {
                $this->redirect($this->createUrl('setting/upload#setting_node_module'));
            }
        }
        $this->render('default');
    }

    //数组转换成字串
    function arrayeval($array, $level = 0) {
        $space = '';
        for($i = 0; $i <= $level; $i++) {
            $space .= "\t";
        }
        $evaluate = "Array\n$space(\n";
        $comma = $space;
        foreach($array as $key => $val) {
            $key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
            $val = !is_array($val) && (!preg_match("/^\-?\d+$/", $val) || strlen($val) > 12 || substr($val, 0, 1)=='0') ? '\''.addcslashes($val, '\'\\').'\'' : $val;
            if(is_array($val)) {
                $evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
            } else {
                $evaluate .= "$comma$key => $val";
            }
            $comma = ",\n$space";
        }
        $evaluate .= "\n$space)";
        return $evaluate;
    }

    //写入文件
    function swritefile($filename, $writetext, $openmod = 'w') {
        if ($fp = fopen($filename, $openmod)) {
            flock($fp, 2);
            fwrite($fp, $writetext);
            fclose($fp);
            return true;
        } else {
            runlog('error', "File: $filename write error.");
            return false;
        }
    }
}